﻿using IRepositoryBll;
using Microsoft.AspNetCore.Mvc;
using RepositoryEntity;
using RepositorySystem.Models;
using RepositorySystem.Models.DepartmentInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RepositorySystem.Controllers
{
    public class DepartmentInfoController : Controller
    {
        private IDepartmentInfoBll _departmentInfoBll;
        private IUserInfoBll _userInfoBll;
        public DepartmentInfoController(IDepartmentInfoBll departmentInfoBll, IUserInfoBll userInfoBll)
        {
            _departmentInfoBll = departmentInfoBll;
            _userInfoBll = userInfoBll;
        }
        public IActionResult DepartmentInfoListView(AddDepartmentInfoModels model)
        {
            return View();
        }
        /// <summary>
        /// 添加部门页面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult AddDepartmentInfoView()
        {
            return View();

        }

        [HttpPost]
        public IActionResult AddDepartmentInfo(AddDepartmentInfoModels model)
        {
            CustomActionResult res = new CustomActionResult();
            if (model.DepartmentName == null)
            {
                res.Msg = "部门名称不能为空";
                return Json(res);
            }
            else if (model.Description == null)
            {
                res.Msg = "部门描述不能为空";
                return Json(res);
            }
            DepartmentInfo departmentInfo = new DepartmentInfo()
            {
                ID = Guid.NewGuid().ToString(),
                DepartmentName = model.DepartmentName,
                Description = model.Description,
                LeaderID = model.LeaderID,
                CreateTime = DateTime.Now,
                ParentID = model.ParentID,
                IsDelete = false
            };
            bool isSuccess = _departmentInfoBll.AddDepartmentInfo(departmentInfo);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Msg = "添加成功";
                return Json(res);

            }
            else
            {
                return Json(res);
            }
        }
        /// <summary>
        /// 删除部门
        /// </summary>
        /// <returns></returns>
        public IActionResult DeleteDepartmentInfo(string departmentInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            if (_departmentInfoBll.SoftDepartmentInfo(departmentInfoId))
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
                return Json(res);
            }
            else
            {
                return Json(res);
            }
        }
        /// <summary>
        /// 获得部门集合
        /// </summary>
        /// <returns></returns>
        public IActionResult GetDepartmentInfoList(int page, int limit, string departmentName)
        {
            int count;
            object departmentInfo = _departmentInfoBll.GetDepartmentInfoListByPage(page, limit, out count, departmentName);
            return Json(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = departmentInfo
            });
            //练习代码Sta
            /* List <DepartmentInfo> departmentInfos = _departmentInfoBll.GetDepartmentInfoList();
             List<object> list = new List<object>();
             foreach (var item in departmentInfos)
             {
                 list.Add(new
                 {

                     item.ID,
                     item.DepartmentName,
                     item.Description,
                     CreateTime = item.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),

                 });
             }
             return Json(new
             {
                 code = 0,
                 msg = "成功",
                 count = departmentInfos.Count,
                 data = list
             });*/
            //练习代码End
        }
        /// <summary>
        /// 获取领导下拉数据集合
        /// </summary>
        /// <returns></returns>
        public IActionResult GetSelectOption()
        {
            CustomActionResult res = new CustomActionResult();
            
            //获取父级部门下拉选数据
            object departmentInfoOptions = _departmentInfoBll.GetDepartmentSelectOption();
            //领导信息下拉数据查询
            object userInfoOptions = _userInfoBll.GetSelectOption();
            res.IsSuccess = true;
            res.Msg = "成功";
            res.Status = 1;
            res.Data = new
            {
                UserInfoOptions = userInfoOptions,
                DepartmentInfoOptions = departmentInfoOptions
            };
            return Json(res);
        }
        /// <summary>
        /// 修改部门页面
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateDepartmentInfoView(string departmentInfoId)
        {

            if (!string.IsNullOrEmpty(departmentInfoId))
            {
                DepartmentInfo departmentInfo = _departmentInfoBll.GetDepartmentInfo(departmentInfoId);
                if (departmentInfoId != null)
                {
                    ViewBag.ID = departmentInfo.ID;
                    ViewBag.DepartmentName = departmentInfo.DepartmentName;
                    ViewBag.Description = departmentInfo.Description;
                }
            }
            return View();
        }
        /// <summary>
        /// 获取更新修改的部门信息
        /// </summary>
        /// <returns></returns>
        public IActionResult GetUpdateDepartmentInfo(string departmentInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            //获取部门信息
            DepartmentInfo departmentInfo = _departmentInfoBll.GetDepartmentInfo(departmentInfoId);
            //领导信息下拉数据查询
            object userInfoOptions = _userInfoBll.GetSelectOption();
            //获取父级部门下拉选数据
            object departmentInfoOptions = _departmentInfoBll.GetDepartmentSelectOption(departmentInfoId);
            res.IsSuccess = true;
            res.Status = 1;
            res.Msg = "成功";
            res.Data = new
            {
                DepartmentInfo = departmentInfo,
                UserInfoOptions = userInfoOptions,
                DepartmentInfoOptions = departmentInfoOptions
            };
            return Json(res);
            
        }
        /// <summary>
        /// 更新部门
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateDepartmentInfo(UpdateDepartmentInfoModels model)
        {
            CustomActionResult res = new CustomActionResult();
            if (_departmentInfoBll.UpdateDepartmentInfo(model.ID,model.DepartmentName,model.Description,model.LeaderID,model.ParentID))
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = "成功";
               
            }
            return Json(res);
        }
    }
}
